var dwpageNo;
var dwid;
var dwpanduan=true;
var dingwei;
// select 基础模拟 !-important 禁止写具体业务 -!
$(".dropdown-menu li").click(function () {
	var bText = $(this).text();
	var bValue = $(this).attr('value');
	//var beforeVal = $(this).parent().siblings('.dropdown-toggle').text();
	//$(this).parent().siblings('.dropdown-toggle').text(beforeVal + bText);
	$(this).parent().siblings('.dropdown-toggle').text(bText);
	$(this).parent().siblings('.dropdown-toggle').find('span').text(bText);
	$(this).parent().parent().find('input[type="hidden"]').val(bValue);
	$(this).parent().css("display","none");
	//$(this).parent().css("display","block");
	return false;
});
//$(document).on("click", "*", function() {
//  $(".r-side").css('display', 'none');
//  $(".routine-diagnosis").css('display', 'none');
//  $('#yibao-ybinfo').css('display', 'none');
//	$(".dropdown-menu").css("display","none")
//});
// 处方类别 li点击事件
$("#a-muban-leibie li, #a-muban-state li").click(function () {
	mingcheng = $("#cfmb_mingcheng").val();
	leibie = $("#cfmb-leibie").val();
	jinyongbz = $("#cfmb-jinyongbz").val();
	pageNumber = 1;
	dwpanduan = false;
    showChufmb();
    return false;
    
});

// 查询条件重置
$(".b-rest").click(function(){
	$('#cfmb_mingcheng').val('');
	$('#cfmb-leibie').val('');
	$("#cfmb-jinyongbz").val('');
	$('.dropdown-toggle').text("");
	var $span = $("<span></span>");
	$('.dropdown-toggle').append($span);
	$('.dropdown-toggle span').text("全部");
	
	mingcheng = $("#cfmb_mingcheng").val();
	leibie = $("#cfmb-leibie").val();
	jinyongbz = $("#cfmb-jinyongbz").val();
	pageNumber=1;
	dwpanduan=false;
    showChufmb();
	return false; 
});

//新建按钮
$(".add").click(function(){
	$("#cedan").load(path+"xjchufangmb.jsp");
	return false; 
});

var mingcheng = '';
var leibie = '';

//查询按钮
$("#cfmb-search").click(function(){
	mingcheng = $("#cfmb_mingcheng").val();
	leibie = $("#cfmb-leibie").val();
	jinyongbz = $("#cfmb-jinyongbz").val();
	pageNumber=1;
	dwpanduan=false;
    showChufmb();
    return false;
});
var jinyongbz = '';
var yuanGongId;
//数据读取
function getCanshu(predwid,predwPage,predwmingcheng,predwleibie, preyuanGongId, prejinyongbz){
	yuanGongId = preyuanGongId;
	if(predwid!='null'&&predwid!=null){
		dwid=predwid;
		mingcheng=predwmingcheng;
		leibie=predwleibie;
		jinyongbz = prejinyongbz;
		$('#cfmb-leibie').val(dwleibie);
		$('#cfmb_mingcheng').val(dwmingcheng);
		$("#cfmb-jinyongbz").val(jinyongbz);
		if(leibie == ''){
			$('#chufmbztwz').text('全部');
		}else if(leibie == 1){
			$('#chufmbztwz').text('成药');
		}else if(leibie == 2){
			$('#chufmbztwz').text('草药');
		}
		if(jinyongbz == ''){
			$('#jinyongbzwz').text('全部');
		}else if(jinyongbz == 1){
			$('#jinyongbzwz').text('启用');
		}else if(jinyongbz == 0){
			$('#jinyongbzwz').text('禁用');
		}
		pageNumber=predwPage;
		showChufmb();
	}else{
		pageNumber=1;
		showChufmb();
	}
}
 function showChufmb() { 
	var totalNumber=0;
    var params={
        "mingcheng":mingcheng,
        "leixing":leibie,
        "jinyongbz": jinyongbz
    };$.ajax({
        type: "get",
        url: path + '/chufangmb/getCount',
        data: params,
    	async: false,
        success: function(data){
	    	totalNumber = data;
	    	if(totalNumber==0){
				$(".c-height").css("display","none");
				$("#cfmb-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
			}else{
				$(".c-height").css("display","block");
				$("#cfmb-img").html("");
			}
    	}
    });
    function createChufmb(name) {
    	$('#tbchufangmb').html('');
		var container = $('#pagination-'+name);
		container.pagination({
		dataSource:'./chufangmb/chufangmbcx?mingcheng='+encodeURI(mingcheng)+'&leixing='+encodeURI(leibie)+'&jinyongbz='+encodeURI(jinyongbz),
		locator: 'entities',
		pageSize: 12,
		pageNumber:pageNumber,
		totalNumber: totalNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			$('#tbchufangmb').html('');
    		var content = '';
            $.each(data,function(index,value){
           	 if(data[index].leixing==2){
       		 	var cflb ="草药";  
             }else if(data[index].leixing==1){
           	 	var cflb ="成药";
             }else{
           	 	var cflb="";
             }
           	if(data[index].jinyongbz==0 ){
       		 var zt="启用";
            }
            else if(data[index].jinyongbz==1 ){
           	   var zt="禁用";
            }
           		if(zt=="启用" ){
       		 var szt="禁用";
            }
            else if(zt=="禁用"){
           	 var  szt="启用";
            }
                content+='<tr id="chufangmbh'+data[index].id+'">'
                +'<td width="6%">'+(((pagination.pageNumber-1)*pagination.pageSize)+index+1)+'</td>'
                +'<td width="30%" style="text-align: left;">' +data[index].mingcheng + "</td>" 
                +'<td width="13%">' + cflb + "</td>"
                +'<td width="13%">' + szt + "</td>" 
                +'<td width="13%">' + (this.xingMing || '')+ "</td>" 
                +'<td width="25%"><a class="operation-left"  id="'+data[index].id+'"  onclick="toChufmbxq('+data[index].id+','+data[index].leixing+','+pagination.pageNumber+')">详情</a>';
                if (this.suoyouzid == yuanGongId) {
	        	    content += '<a class="operation-middle" value="'+zt+'" index="'+data[index].id+'" onclick="jingyongbz('+data[index].id+','+data[index].jinyongbz+','+pagination.pageNumber+',$(this))">'+zt+'</a>'
	        		+'<a class="operation-right"  onclick="cfmbquxiao2('+data[index].id+',$(this))">删除</a></td>'
                }
                content += "</tr><tr></tr>";
				if(dwid==this.id) {
					dingwei=this.id;
				}
            });
            $("#tbchufangmb").html(content);
            //查询状态定位
        	if(dingwei!=null&&dingwei!=''){
 	        	$('#tbchufangmb').find('tr[id="chufangmbh'+dingwei+'"]').addClass('list-tr');
        	}
			//点击查询按钮和重置按钮定位状态消失
        	if(!dwpanduan){
        		$('#tbchufangmb').find('tr[id="chufangmbh'+dingwei+'"]').removeClass('list-tr');
       	 	}
        }
    	});
		 return container;
    }
    createChufmb('fenychufangmb');
		if(totalNumber <= 12) {
			$('#pagination-fenychufangmb').css('display','none');
		}else{
			$('#pagination-fenychufangmb').css('display','block');
		}
}

var row_id = 0;
function jingyongbz(id,sf,prepage,$this){
	$this.parent().parent().siblings('tr').removeClass('list-tr');
	dwpage=prepage;
	row_id= id;
	 if($(".operation-middle[index='"+id+"']").attr("value")== "禁用" ){
		$("#awaken-cfmb").modal(); 
	 }else{
    	$("#prohibit-cfmb").modal();
     }	 
}

//弹出框
function confrim(){
	 if($(".operation-middle[index='"+row_id+"']").attr("value")== "禁用" ){
         var jinyongbz=0;
     }else if($(".operation-middle[index='"+row_id+"']").attr("value")== "启用"  ){
    	 var jinyongbz=1;
     }
	 var sendData1 ={
		 "id":row_id,
		 "jinyongbz":jinyongbz
	 };
	  $.ajax({
	        //请求方式为get
	        type:"POST",
	        //获取json文件路径
	        url:"./chufangmb/jinyongmb",
	        //传送数据
	        data:sendData1,
	        //请求成功完成后要执行的方法
	        success: function(data){
		  	     $("#prohibit-cfmb").modal('hide');
	        	 $("#awaken-cfmb").modal('hide');
		  		if (data == MSG_CHONGFUBZ) {
		  			notifyWarning('该诊所已存在该处方模板');
		  			return false;
		  		}
	        	 pageNumber=dwpage;
	        	 showChufmb();
	        	 if(jinyongbz == 0){
	        		 notifySuccess("禁用处方成功");
	        	 }else if(jinyongbz == 1){
	        		 notifySuccess("启用处方成功");
	        	 }
	        }
	    });
	  return false;
}
//弹出框确认
$("#awaken-cfmb .confirm").click(function(){
	confrim();
});
$("#prohibit-cfmb .confirm").click(function(){
	confrim();
});
//弹出框取消
$(".canceal").click(function(){
	 $("#prohibit-cfmb").modal('hide');
	 $("#awaken-cfmb").modal('hide');
});
function cancelQuery() {
	window.location.href = document.referrer;
}

function toChufmbxq(id,leixing,prepageNo){
	var dwmingcheng=$('#cfmb_mingcheng').val();
	var dwleibie=$('#cfmb-leibie').val();
	dwmingcheng=encodeURI(encodeURI(dwmingcheng));
	if(leixing==1) {
		$("#cedan").load(path+"chufangmbxq.jsp?ID="+id+"&dwPage="+prepageNo+"&dwmingcheng="+dwmingcheng+"&dwleibie="+dwleibie + '&jinyongbz=' + jinyongbz);
	}else {
		$("#cedan").load(path+"chufangmbxq.jsp?ID="+id+"&dwPage="+prepageNo+"&dwmingcheng="+dwmingcheng+"&dwleibie="+dwleibie + '&jinyongbz=' + jinyongbz);
	}
	return false; 
}
var as_id = 0;
function cfmbquxiao2(id,$this){
	$this.parent().parent().siblings('tr').removeClass('list-tr');
	as_id=id;
    $("#delete").modal(); 	
}

//删除弹出框
function delete_Confrim(){
	 var sendData={
		 "id":as_id,
	 };
	 $.ajax({
         type:"POST",
         url:'./chufangmb/shanchucfmb',
         data:sendData,
         success:function(data){
        	 $("#delete").modal('hide'); 
     	  	showChufmb();
     	  	notifySuccess("删除处方成功");
            return false;
         }
       });    
  
}
//弹出框确认
$("#delete .confirm").click(function(){
	delete_Confrim();
});

//弹出框取消
$(".canceal").click(function(){
	 $("#prohibit").modal('hide');
	 $("#awaken").modal('hide');
	 $("#prohibit-gly").modal('hide');
	 $("#delete").modal('hide'); 	
});
// 通过名称和类型查询诊所中是否存在重名
function compareChuFangmbBymcAndlx(strParams) {
	var isZhensuo = true;
	$.ajax({
		type: "get",
		data: {"strParams": strParams},
		url: path + "chufangmb/compareChuFangmbBymcAndlx",
		success: function (data) {
			isZhensuo = data;
		}
	});
	return isZhensuo;
} 
